/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.apps.epocket.stripes;

import com.apps.epocket.dao.DokumenPengiklananDAO;
import com.apps.epocket.model.DokumenPengiklanan;
import com.google.inject.Inject;
import java.io.File;
import java.io.FileInputStream;
import net.sourceforge.stripes.action.*;

/**
 *
 * @author fikri
 */
@UrlBinding("/dokumen/view/{idImg}")
public class ViewDocument extends BaseActionBean {

    @Inject
    private Configuration conf;
    
    @Inject
    private DokumenPengiklananDAO dokumenPengiklananDAO;
    
    private Integer idImg;

    @DefaultHandler
    public Resolution view() {

        String docPath = conf.getProperty("dms.path");
        if (docPath == null) {
            return new ErrorResolution(500,
                    "Configuration of \"dms.path\" not found!!");
        }

        DokumenPengiklanan img = dokumenPengiklananDAO.findById(idImg);

        String path = docPath + (docPath.endsWith(File.separator) ? "" : File.separator) + img.getFizikalLokasi();
        File f = new File(path);
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(f);

        } catch (Exception e) {

            return new ErrorResolution(500,
                    "Fail " + path + " tidak dijumpai");
        }

        return new StreamingResolution(img.getFormat(), fis);

    }

    public Integer getIdImg() {
        return idImg;
    }

    public void setIdImg(Integer idImg) {
        this.idImg = idImg;
    }
}
